/*
 * SAMER08D.cxx
 *
 *  Created on: 6:16:25 PM Sep 26, 2012
 *  Author: Pallab
 *
 * "I have not failed, I have just found 10000 ways that won't work.
 */

#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <functional>
#include <bitset>
#include <iomanip>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;

#define foR(i1,st,ed) for(int i1 = st , j1 = ed ; i1 < j1 ; ++i1 )
#define fo(i1,ed) foR( i1 , 0 , ed )
#define foE(i1,st,ed) foR( i1, st, ed+1 )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )
#define line puts("")

inline void wait(double seconds) {
	double endtime = clock() + (seconds * CLOCKS_PER_SEC);
	while (clock() < endtime) {
		;
	}
}
#define T int
inline T fastIn() {
	register char c = 0;
	register T a = 0;
	while (c < 33)
		c = getchar();
	while (c > 33) {
		a = (a * 10) + (c - '0');
		c = getchar();
	}
	return a;
}
int K;
const int mx = 1001;
char s1[mx];
char s2[mx];
int ls1, ls2;
int dp[mx][mx];
int go(int i, int j) {
	if (i >= ls1 || j >= ls2)
		return 0;
	int &bst = dp[i][j];
	if(bst!=-1)return bst;
	bst=0;

	int v1 = go(i + 1, j);
	bst = max(bst, v1);
	int v2 = go(i, j + 1);
	bst = max(bst, v2);

	int match_count = 0;
	while (i < ls1 && j < ls2) {
		if (s1[i] == s2[j]) {
			++match_count;
		} else {
			break;
		}
		if (match_count >= K) {
			int v3 = match_count + go(i + 1, j + 1);
			bst = max(bst, v3);
		}
		++i;
		++j;
	}
	return bst;
}
inline void solve() {
	SET(dp,-1);
	int res = go(0,0);
	cout<<res<<'\n';
}
int main() {
	int kase = 1;
#if defined( xerxes_pc )
	if (!freopen("D/in1", "r", stdin))
		puts("error opening file SAMER08D/in "), assert(0);
	kase = 1;
#endif
	while (scanf("%d", &K) == 1) {
		if (K == 0)
			break;
		scanf("\n");
		gets(s1);
		gets(s2);
		ls1 = strlen(s1);
		ls2 = strlen(s2);
		solve();
	}
	return 0;
}
